<template>
	<view class="addressEdit">
		<navBar :title="title"></navBar>
		<view class="box form">
			<!-- <u-form :model="form" :border-bottom="false" label-width="150" :rules="rules" ref="uForm"
				:error-type="['message']">
				<u-form-item label="收货人" prop="name" class="item">
					<u-input v-model="form.name" placeholder="请输入收货人名称" />
				</u-form-item>
				<u-form-item label="手机号码" prop="mobile" class="item">
					<u-input v-model="form.mobile" placeholder="请输入联系方式" />
				</u-form-item>
				<u-form-item label="所在地区" prop="city" class="item">
					<u-input type="select" v-model="form.city" :select-open="pickerShow" placeholder="请选择地区"
						@click="pickerShow = true" />
				</u-form-item>
				<u-form-item label="详细地址" prop="city_details" class="item">
					<u-input placeholder="请输入详细地址" v-model="form.city_details" />
				</u-form-item>
				<u-form-item label="设为默认地址" label-width="200">
					<u-switch v-model="form.city_id0 slot="right" size="35" active-color="#CB712B"></u-switch>
				</u-form-item>
			</u-form> -->
			<view class="item">
				<view class="label">
					收货人
				</view>
				<view class="right">
					<u-input v-model="form.name" placeholder="请输入收货人名称" />
				</view>
			</view>
			<view class="item">
				<view class="label">
					联系方式
				</view>
				<view class="right">
					<u-input v-model="form.mobile" placeholder="请输入联系方式" />
				</view>
			</view>
			<view class="item">
				<view class="label">
					所在地区
				</view>
				<view class="right" @click="choosePo">
					<text v-if="form.city">{{form.city}}</text>
					<text style="color: #999;" v-else>请选择所在地区</text>
					<!-- <u-input v-model="form.city" :disabled="true" placeholder="请输入所在地区" /> -->
					<view class="flex-btw" style="white-space: nowrap;">
						<u-icon name="map" color="#999999"></u-icon>
						<text>定位</text>
					</view>
				</view>
			</view>
			<view class="item">
				<view class="label">
					详细地址
				</view>
				<view class="right">
					<u-input v-model="form.city_details" placeholder="请输入详细地址" />
				</view>
			</view>
			<view class="item">
				<view class="left">
					<text class="name">设置默认地址</text>
					<text>提醒：每次下单会默认推荐使用该地址</text>
				</view>
				<u-switch v-model="form.city_id" size="35" active-color="#CB712B"></u-switch>
			</view>
		</view>
		<!-- <view class="addressEdit-btn"> 设为默认 </view> -->
		<view class="btns">
			<view class="addressEdit-btn" @click="submit"> 保存地址 </view>
			<view class="addressEdit-btn" @click="addressDel(id)" v-if="id!=-1"> 删除地址 </view>
		</view>
		<!-- <semp-city @confirm="onCityConfirm"> </semp-city> -->
		<u-picker mode="region" v-model="pickerShow" @confirm="regionConfirm"></u-picker>
	</view>
</template>

<script>
	// import cityData from "@/utils/city.data.js";
	// import SempCity from "@/components/semp-city/semp-city";
	import {
		address,
		addressDelete
	} from "@/utils/api";
	import {
		mapActions,
		mapGetters
	} from "vuex";
	export default {
		// components: {
		// 	SempCity,
		// },
		computed: {
			...mapGetters(["eidtAddressInfo"]),
		},
		data() {
			return {
				rules: {
					name: [{
							required: true,
							message: "请输入收货人名称",
							trigger: "blur",
						},
						{
							min: 1,
							max: 5,
							message: "名称长度在1到5个字符",
							trigger: ["change", "blur"],
						},
					],
					mobile: [{
						required: true,
						message: "请输入联系方式",
						trigger: "blur",
					}, ],
					city: [{
						required: true,
						message: "请选择收货地区",
						trigger: "change",
					}, ],
					city_details: [{
						required: true,
						message: "请输入详细地址",
						trigger: "blur",
					}, ],
				},
				type: 0, // 地址操作类型
				id: "", // 编辑地址id标记
				form: {
					id: "",
					name: "",
					mobile: "",
					city: "",
					city_details: "", // 详细地址
					city_id: false, // 是否默认
				},
				checked: true,
				pickerShow: false,
				title: ''
			};
		},
		onLoad({
			type,
			id
		}) {
			console.log(type, id);
			this.type = type;
			this.id = id;
			// 编辑 获取vuex中的地址值，进行初始赋值
			if (this.type == "edit") {
				Object.keys(this.form).forEach((key) => {
					this.form[key] = this.eidtAddressInfo[key];
				});
				this.form.city_id = this.form.city_id ? true : false
				// this.form=this.eidtAddressInfo
			}
		},
		onReady() {
			// this.$refs.uForm.setRules(this.rules);
			var that = this;
			this.title = that.type == "edit" ? "编辑收货人" : "新增收货人"
		},
		methods: {
			...mapActions(["recordEditAddress"]),
			regionConfirm(e) {
				this.form.city =
					e.province.label + " " + e.city.label + " " + e.area.label;
			},
			choosePo() {
				uni.chooseLocation({
					success: (res) => {
						this.form.city = res.address
					},
					fail: (err) => {
						console.log(err);
					}
				})
			},
			// 保存地址
			submit() {
				var that = this;
				var extraKey = ['city_id', 'id', 'city_details']
				for(var key in this.form){
					if((!this.form[key]) && extraKey.indexOf(key) === -1){
						this.$utils.toast('请将信息填写完整')
						return
					}
				}
				const {
					id,
					name,
					mobile,
					city,
					city_details,
					city_id
				} = that.form;
				address({
					id, // 添加不传id，编辑传id
					name,
					mobile,
					city,
					city_details,
					city_id: city_id ? 1 : 0, // 1-默认地址 0-非默认
				}).then((resp) => {
					console.log("地址操作", resp);
					if (resp.code == 200) {
						uni.showToast({
							title: resp.msg,
							icon: "none",
							success: () => {
								setTimeout(() => {
									this.$Router.back(1)
								}, 1500);
							},
						});
					}
				});
			},
			// 删除地址
			addressDel(id) {
				addressDelete({
					id
				}).then((resp) => {
					console.log("地址删除", resp);
					if (resp.code == 200) {
						uni.showToast({
							title: resp.msg,
							icon: "none",
							success: () => {
								setTimeout(() => {
									uni.redirectTo({
										url: "/pages/common/address/address",
									});
								}, 1500);
							},
						});
					}
				});
			},
		},
	};
</script>

<style lang="scss">
	page {
		background: #f3f3f3;
	}

	.addressEdit {
		width: 100%;
		box-sizing: border-box;

		&-btn {
			line-height: 73rpx;
			text-align: center;
			width: 100%;
			background-color: #ffffff;
			font-size: 36rpx;
			margin-top: 30rpx;
			border-radius: 16rpx;
			letter-spacing: 1rpx;
			margin: 15rpx auto;
			color: #ffffff;
			background: linear-gradient(to right, #FFBC67, #FC602B);
			font-size: 28rpx;
			border-radius: 50rpx;
			width: 680rpx;
		}

		.btns {
			position: absolute;
			left: 0;
			width: 100%;
			bottom: 50rpx;
		}

		.box {
			border-radius: 0 0 20rpx 20rpx;
		}
	}

	.form {
		.item {
			margin-bottom: 20rpx;
			padding-bottom: 20rpx;
			@extend .flex-btw;
			border-bottom: 1px solid #EEEFF0;
			font-size: 28rpx;

			&:last-child {
				margin-bottom: 0;
				border: none;
			}

			input {
				width: 100%;
			}

			.label {
				width: 112rpx;
			}

			.right {
				margin-left: 39rpx;
				flex: 1;
				@extend .flex-btw;
			}

			.left {
				display: flex;
				flex-direction: column;
				font-size: 24rpx;
				color: #999999;

				.name {
					font-size: 32rpx;
					color: #333333;
					margin-bottom: 10rpx;
				}
			}
		}
	}
</style>
